package com.action;

import com.beans.Manufacturer;
import com.beans.Product;
import com.hibernate.HibernateUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;

public class SearchAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private List<Manufacturer> ManuResult;
	private List<Product> ProductResult;
	private Manufacturer provider;

	public String execute() {
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String quickSearch(){
		
		String ResultType= ServletActionContext.getRequest().getParameter("SearchType");
		String KeyWord = ServletActionContext.getRequest().getParameter("SearchedKeyWord");
		if(!KeyWord.equals("")){
		
		ServletActionContext.getRequest().setAttribute("result", "Search Results for the key word: \""+KeyWord+"\"");
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			if(ResultType.equals("Manufacturer")){
			
				int validId;
				try{validId=Integer.parseInt(KeyWord);}
				catch(NumberFormatException e){validId=0;}
				
				Criteria criteria = session.createCriteria(Manufacturer.class);
				criteria.add(Restrictions.disjunction()
						.add(Restrictions.eq("id", validId))
						.add(Restrictions.like("name","%"+KeyWord+"%"))
						.add(Restrictions.like("addr","%"+KeyWord+"%"))
						.add(Restrictions.like("telephone","%"+KeyWord+"%"))
						.add(Restrictions.like("des","%"+KeyWord+"%"))
				);
				
				ManuResult=criteria.list();
				if(ManuResult.size()==0){ServletActionContext.getRequest().setAttribute("result", "There is no matchings for the key word: \""+KeyWord+"\"");};

			} else { if(ResultType.equals("Product")) {
				
						String ManuId=ServletActionContext.getRequest().getParameter("manuid");
						int validProductId,validManuId,validQuantity;
						long validPrice;
						validManuId=Integer.parseInt(ManuId);
						try{
							validProductId=Integer.parseInt(KeyWord);
							validQuantity=Integer.parseInt(KeyWord);
							}
						catch(NumberFormatException e){
							validProductId=0;
							validQuantity=0;
							}
						try{validPrice=Long.parseLong(KeyWord);}
						catch(NumberFormatException e){validPrice=0;}
						
						provider= (Manufacturer) session.get(Manufacturer.class, validManuId);
						Criteria criteria = session.createCriteria(Product.class);
						criteria.add(Restrictions.and(Restrictions.eq("manufacturer", provider), 
								Restrictions.disjunction()
								.add(Restrictions.eq("productID", validProductId))
								.add(Restrictions.eq("quantity",validQuantity))
								.add(Restrictions.eq("price", validPrice))
								.add(Restrictions.like("des", "%"+KeyWord+"%"))
								.add(Restrictions.like("name", "%"+KeyWord+"%")))
								);
						
						ProductResult=criteria.list();
						if(ProductResult.size()==0){ServletActionContext.getRequest().setAttribute("result", "There is no matchings for the key word: \""+KeyWord+"\"");};
				 	 } else {
				 		 System.out.println("Unclear Search Type"); 
				 	 }
			}	
		
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		} 
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String advanceSearch(){
		String Searchtype=ServletActionContext.getRequest().getParameter("searchtype");
		
		
			
			if(Searchtype.equals("1")){ //Manufacturer
				
				Session session = HibernateUtil.getSessionFactory().openSession();
				Transaction transaction = null;
				try{
				transaction = session.beginTransaction();
				
				String ManuId=ServletActionContext.getRequest().getParameter("manuid");
				Criteria criteria = session.createCriteria(Manufacturer.class);
				
				//Manufacturer ID:
				int validManuId;
				String opp;
				int convertopp=0;
				int validManuIdExtra;
				String ManuIdExtra="";
				try{
					validManuId=Integer.parseInt(ManuId);
				}catch(NumberFormatException e){
					validManuId=-1;
				}
				if(validManuId!=-1){
					
					opp=ServletActionContext.getRequest().getParameter("manuidopp");
					if(opp.equals("7")){
						ManuIdExtra=ServletActionContext.getRequest().getParameter("manuidextra");
						try{
							validManuIdExtra=Integer.parseInt(ManuIdExtra);
						}catch(NumberFormatException e){
							validManuIdExtra=-1;
						}
						if(validManuIdExtra!=-1){
							criteria.add(Restrictions.between("id",validManuId,validManuIdExtra));
						}
					}else{
						convertopp=Integer.parseInt(opp);
						switch (convertopp){
							case 1:{criteria.add(Restrictions.eq("id",validManuId));break;}
							case 2:{criteria.add(Restrictions.lt("id",validManuId));break;}
							case 3:{criteria.add(Restrictions.le("id",validManuId));break;}
							case 4:{criteria.add(Restrictions.ge("id",validManuId));break;}
							case 5:{criteria.add(Restrictions.gt("id",validManuId));break;}
							case 6:{criteria.add(Restrictions.ne("id",validManuId));break;}
						}
					}
				}
					
				//Name :
				String name=ServletActionContext.getRequest().getParameter("name");
				if(!name.equals("")){
					criteria.add(Restrictions.like("name","%"+name+"%"));
				}
				
				//Address :
				String address=ServletActionContext.getRequest().getParameter("addr");
				if(!address.equals("")){
					criteria.add(Restrictions.like("addr","%"+address+"%"));
				}
					
				//Telephone :
				String telenumber=ServletActionContext.getRequest().getParameter("telephone");
				if(!telenumber.equals("")){
					criteria.add(Restrictions.like("telephone","%"+telenumber+"%"));
				}
					
				//Description :
				String Description=ServletActionContext.getRequest().getParameter("des");
				if(!Description.equals("")){
					criteria.add(Restrictions.like("des","%"+Description+"%"));
				}
					
				ManuResult=criteria.list();
				if(ManuResult.size()==0){ServletActionContext.getRequest().setAttribute("result", "There is no matchings for that combination");}
				else{ ServletActionContext.getRequest().setAttribute("result", "Search Results: ");}
				
				transaction.commit();
				}catch(HibernateException e){
					transaction.rollback();
					e.printStackTrace();
				}finally{
					session.close();
				}
				return "manufacturer";
				
			}else{
				if(Searchtype.equals("2")){ //Product
					
					Session session = HibernateUtil.getSessionFactory().openSession();
					Transaction transaction = null;
					try{
					transaction = session.beginTransaction();
					
					Criteria criteria = session.createCriteria(Product.class);
					
					//Product ID : 
					String ProductId=ServletActionContext.getRequest().getParameter("productid");
					int validProductId;
					try{
						validProductId=Integer.parseInt(ProductId);
					}catch(NumberFormatException e){
						validProductId=-1;
					}
					if(validProductId!=-1){
						String opp=ServletActionContext.getRequest().getParameter("productidopp");
						if(opp.equals("7")){
							String ProductIdExtra=ServletActionContext.getRequest().getParameter("productidextra");
							int validProductIdExtra;
							try{
								validProductIdExtra=Integer.parseInt(ProductIdExtra);
							}catch(NumberFormatException e){
								validProductIdExtra=-1;
							}
							if(validProductIdExtra!=-1){
								criteria.add(Restrictions.between("productID",validProductId,validProductIdExtra));
							}
						}else{
							int convertopp=Integer.parseInt(opp);
							switch (convertopp){
								case 1:{criteria.add(Restrictions.eq("productID",validProductId));break;}
								case 2:{criteria.add(Restrictions.lt("productID",validProductId));break;}
								case 3:{criteria.add(Restrictions.le("productID",validProductId));break;}
								case 4:{criteria.add(Restrictions.ge("productID",validProductId));break;}
								case 5:{criteria.add(Restrictions.gt("productID",validProductId));break;}
								case 6:{criteria.add(Restrictions.ne("productID",validProductId));break;}
							}
						}
					}
					
					//Name :
					String name=ServletActionContext.getRequest().getParameter("prname");
					if(!name.equals("")){
						criteria.add(Restrictions.like("name","%"+name+"%"));
					}
					
					//Price :
					String Price=ServletActionContext.getRequest().getParameter("price");
					long validPrice;
					try{
						validPrice=Long.parseLong(Price);
					}catch(NumberFormatException e){
						validPrice=-1;
					}
					if(validPrice!=-1){
						String opp=ServletActionContext.getRequest().getParameter("priceopp");
						if(opp.equals("7")){
							String PriceExtra=ServletActionContext.getRequest().getParameter("priceextra");
							long validPriceExtra;
							try{
								validPriceExtra=Long.parseLong(PriceExtra);
							}catch(NumberFormatException e){
								validPriceExtra=-1;
							}
							if(validPriceExtra!=-1){
								criteria.add(Restrictions.between("price",validPrice,validPriceExtra));
							}
						}else{
							int convertopp=Integer.parseInt(opp);
							switch (convertopp){
								case 1:{criteria.add(Restrictions.eq("price",validPrice));break;}
								case 2:{criteria.add(Restrictions.lt("price",validPrice));break;}
								case 3:{criteria.add(Restrictions.le("price",validPrice));break;}
								case 4:{criteria.add(Restrictions.ge("price",validPrice));break;}
								case 5:{criteria.add(Restrictions.gt("price",validPrice));break;}
								case 6:{criteria.add(Restrictions.ne("price",validPrice));break;}
							}
						}
					}
					
					//Quantity :
					String Quantity=ServletActionContext.getRequest().getParameter("quantity");
					int validQuantity;
					try{
						validQuantity=Integer.parseInt(Quantity);
					}catch(NumberFormatException e){
						validQuantity=-1;
					}
					if(validQuantity!=-1){
						String opp=ServletActionContext.getRequest().getParameter("quantityopp");
						if(opp.equals("7")){
							String QuantityExtra=ServletActionContext.getRequest().getParameter("quantityextra");
							int validQuantityExtra;
							try{
								validQuantityExtra=Integer.parseInt(QuantityExtra);
							}catch(NumberFormatException e){
								validQuantityExtra=-1;
							}
							if(validQuantityExtra!=-1){
								criteria.add(Restrictions.between("quantity",validQuantity,validQuantityExtra));
							}
						}else{
							int convertopp=Integer.parseInt(opp);
							switch (convertopp){
								case 1:{criteria.add(Restrictions.eq("quantity",validQuantity));break;}
								case 2:{criteria.add(Restrictions.lt("quantity",validQuantity));break;}
								case 3:{criteria.add(Restrictions.le("quantity",validQuantity));break;}
								case 4:{criteria.add(Restrictions.ge("quantity",validQuantity));break;}
								case 5:{criteria.add(Restrictions.gt("quantity",validQuantity));break;}
								case 6:{criteria.add(Restrictions.ne("quantity",validQuantity));break;}
							}
						}
					}
					
					//Description :
					String Description=ServletActionContext.getRequest().getParameter("prdes");
					if(!Description.equals("")){
						criteria.add(Restrictions.like("des","%"+Description+"%"));
					}
					
					ProductResult=criteria.list();
					if(ProductResult.size()==0){ServletActionContext.getRequest().setAttribute("result", "There is no matchings for that combination");}
					else{ ServletActionContext.getRequest().setAttribute("result", "Search Results: ");}
					
					ServletActionContext.getRequest().setAttribute("noreturn" ,"true" );
					ServletActionContext.getRequest().setAttribute("defaultselect", "product");
					
					transaction.commit();
					}catch(HibernateException e){
						transaction.rollback();
						e.printStackTrace();
					}finally{
						session.close();
					}
					return "product";
					
				} else{System.out.println("Unidentified Searchtype"); return "fail";}
			}
	}

	public void setManuResult(List<Manufacturer> manuResult) {
		ManuResult = manuResult;
	}

	public List<Manufacturer> getManuResult() {
		return ManuResult;
	}

	public void setProductResult(List<Product> productResult) {
		ProductResult = productResult;
	}

	public List<Product> getProductResult() {
		return ProductResult;
	}
	
	public Manufacturer getProvider(){
		return this.provider;
	}
	public void setProvider(Manufacturer e){
		this.provider = e;
	}
}
